<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="delAll"
          >批量删除</el-button
        >
        <el-input
          v-model="select_word"
          size="mini"
          placeholder="请输入村户户名"
          class="handle-input"
        ></el-input>
        <el-button
          type="primary"
          size="mini"
          @click="centerDialogVisible = true"
          >添加走访户工作信息</el-button
        >
      </div>
    </div>
    <!-- 查询 -->
    <el-table
      size="mini"
      ref="multipleTable"
      border
      style="width: 100%"
      height="570px"
      :data="data"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column
        prop="name"
        label="村户户主"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="dateVisit"
        label="走访日期"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="familySituation"
        label="家庭情况"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="familySituationIllustrate"
        label="家庭情况说明"
        width="200"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="popularSentiment"
        label="民情心声"
        width="193"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="implementationStatus"
        label="民情落实"
        width="193"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="employmentChildren"
        label="子女就业"
        width="193"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        width="193"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        background
        layout="total,prev,pager,next"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>

    <!-- 添加 -->
    <el-dialog
      title="添加走访户工作信息"
      :visible.sync="centerDialogVisible"
      width="400px"
      center
    >
      <el-form :model="registerForm" ref="registerForm" label-width="80px">
        <el-form-item prop="name" label="村户户主" size="mini">
          <el-input
            v-model="registerForm.name"
            placeholder="村户户主"
          ></el-input>
        </el-form-item>
        <el-form-item label="走访日期" size="mini">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="registerForm.dateVisit"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item prop="familySituation" label="家庭情况" size="mini">
          <el-cascader
            :options="options"
            v-model="registerForm.familySituation"
            placeholder="家庭情况"
          ></el-cascader>
        </el-form-item>
        <el-form-item
          prop="familySituationIllustrate"
          label="家庭情况说明"
          size="mini"
        >
          <el-input
            v-model="registerForm.familySituationIllustrate"
            placeholder="家庭情况说明"
            type="textarea"
          ></el-input>
        </el-form-item>

        <el-form-item prop="popularSentiment" label="民情心声" size="mini">
          <el-input
            v-model="registerForm.popularSentiment"
            placeholder="民情心声"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item prop="implementationStatus" label="民情落实" size="mini">
          <el-input
            v-model="registerForm.implementationStatus"
            placeholder="民情落实"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item prop="employmentChildren" label="子女就业" size="mini">
          <el-input
            v-model="registerForm.employmentChildren"
            placeholder="子女就业"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item prop="remark" label="备注" size="mini">
          <el-input
            v-model="registerForm.remark"
            placeholder="备注"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="centerDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" @click="addVisitorWork">确定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      title="修改走访户工作信息"
      :visible.sync="editVisible"
      width="400px"
      center
    >
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item prop="name" label="村户户主" size="mini">
          <el-input v-model="form.name" placeholder="村户户主"></el-input>
        </el-form-item>
        <el-form-item label="走访日期" size="mini">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.dateVisit"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item prop="familySituation" label="家庭情况" size="mini">
          <el-cascader
            :options="options"
            v-model="form.familySituation"
            placeholder="家庭情况"
          ></el-cascader>
        </el-form-item>
        <el-form-item
          prop="familySituationIllustrate"
          label="家庭情况说明"
          size="mini"
        >
          <el-input
            v-model="form.familySituationIllustrate"
            placeholder="家庭情况说明"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item prop="popularSentiment" label="民情心声" size="mini">
          <el-input
            v-model="form.popularSentiment"
            placeholder="民情心声"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item prop="implementationStatus" label="民情落实" size="mini">
          <el-input
            v-model="form.implementationStatus"
            placeholder="民情落实"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item prop="employmentChildren" label="子女就业" size="mini">
          <el-input
            v-model="form.employmentChildren"
            placeholder="子女就业"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item prop="remark" label="备注" size="mini">
          <el-input
            v-model="form.remark"
            placeholder="备注"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editVisible = false">取消</el-button>
        <el-button size="mini" @click="editVisitorWork">确定</el-button>
      </span>
    </el-dialog>

    <!-- 删除 -->
    <el-dialog
      title="删除走访户工作信息"
      :visible.sync="delVisible"
      width="300px"
      center
    >
      <div align="center">删除不可恢复，是否确定删除？</div>
      <span slot="footer">
        <el-button size="mini" @click="delVisible = false">取消</el-button>
        <el-button size="mini" @click="deleteRow">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getVisitorWork,
  setVisitorWork,
  updateVisitorWork,
  deleteVisitorWork,
} from "../api/index";
import { mixin } from "../mixins/index";
export default {
  mixins: [mixin],
  data() {
    return {
      options: [
        {
          value: "一般",
          label: "一般",
        },
        {
          value: "困难",
          label: "困难",
        },
        {
          value: "极度困难",
          label: "极度困难",
        },
        {
          value: "建档立卡户",
          label: "建档立卡户",
        },
        {
          value: "正常",
          label: "正常",
        },
      ],
      centerDialogVisible: false, //添加弹窗是否显示
      editVisible: false, //编辑弹窗是否显示
      delVisible: false, //删除弹窗是否显示
      tableData: [],
      tempData: [],
      select_word: "",
      pageSize: 13, //分页每页大小
      currentPage: 1, //当前页
      idx: -1, //当前选择项
      multipleSelection: [], //哪些项已经打勾
      // 添加框
      registerForm: {
        name: "",
        dateVisit: "",
        familySituation: "",
        familySituationIllustrate: "",
        popularSentiment: "",
        implementationStatus: "",
        employmentChildren: "",
        remark: "",
      },
      // 编辑框
      form: {
        id: "",
        name: "",
        dateVisit: "",
        familySituation: "",
        familySituationIllustrate: "",
        popularSentiment: "",
        implementationStatus: "",
        employmentChildren: "",
        remark: "",
      },
    };
  },
  computed: {
    //计算当前搜索结果表里的数据
    data() {
      return this.tableData.slice(
        (this.currentPage - 1) * this.pageSize,
        this.currentPage * this.pageSize
      );
    },
  },
  watch: {
    //搜索框里面的内容发生变化的时候，搜索结果table列表的内容跟着它的内容发生变化
    select_word: function () {
      if (this.select_word === "") {
        this.tableData = this.tempData;
      } else {
        this.tableData = [];
        for (let item of this.tempData) {
          if (item.name.includes(this.select_word)) {
            this.tableData.push(item);
          }
        }
      }
    },
  },
  created() {
    this.getData();
  },
  methods: {
    //获取当前页
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    getData() {
      this.tempData = [];
      this.tableData = [];
      getVisitorWork().then((res) => {
        this.tempData = res;
        this.tableData = res;
      });
    },
    // 添加走访户工作信息
    addVisitorWork() {
      let d = this.registerForm.dateVisit;
      let datetime =
        d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
      let params = new URLSearchParams();
      params.append("name", this.registerForm.name);
      params.append("dateVisit", datetime);
      params.append("familySituation", this.registerForm.familySituation);
      params.append(
        "familySituationIllustrate",
        this.registerForm.familySituationIllustrate
      );
      params.append("popularSentiment", this.registerForm.popularSentiment);
      params.append(
        "implementationStatus",
        this.registerForm.implementationStatus
      );
      params.append("employmentChildren", this.registerForm.employmentChildren);
      params.append("remark", this.registerForm.remark);

      setVisitorWork(params)
        .then((res) => {
          if (res.code === 1) {
            this.getData();
            this.notify("添加成功", "success");
          } else {
            this.notify("添加失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.centerDialogVisible = false;
    },
    // 弹出编辑页面
    handleEdit(row) {
      this.editVisible = true;
      this.form = {
        id: row.id,
        name: row.name,
        dateVisit: row.dateVisit,
        familySituation: row.familySituation,
        familySituationIllustrate: row.familySituationIllustrate,
        popularSentiment: row.popularSentiment,
        implementationStatus: row.implementationStatus,
        employmentChildren: row.employmentChildren,
        remark: row.remark,
      };
    },
    // 保存编辑页面修改的内容
    editVisitorWork() {
      let d = new Date(this.form.dateVisit);
      let datetime =
        d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
      let params = new URLSearchParams();
      params.append("id", this.form.id);
      params.append("name", this.form.name);
      params.append("dateVisit", datetime);
      params.append("familySituation", this.form.familySituation);
      params.append(
        "familySituationIllustrate",
        this.form.familySituationIllustrate
      );
      params.append("popularSentiment", this.form.popularSentiment);
      params.append("implementationStatus", this.form.implementationStatus);
      params.append("employmentChildren", this.form.employmentChildren);
      params.append("remark", this.form.remark);

      updateVisitorWork(params)
        .then((res) => {
          if (res.code === 1) {
            this.getData();
            this.notify("修改成功", "success");
          } else {
            this.notify("修改失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.editVisible = false;
    },
    // 删除走访户工作信息
    deleteRow() {
      deleteVisitorWork(this.idx)
        .then((res) => {
          if (res) {
            this.getData();
            this.notify("删除成功", "success");
          } else {
            this.notify("删除失败", "error");
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.delVisible = false;
    },
  },
};
</script>
<style scoped>
.handle-box {
  margin-bottom: 20px;
}
.handle-input {
  width: 300px;
  display: inline-block;
}
.pagination {
  display: flex;
  justify-content: center;
}
</style>
